package com.wuuxiang.polestar.admin.service.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.curator.utils.ZKPaths;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wuuxiang.polestar.admin.dao.IPropertyDao;
import com.wuuxiang.polestar.admin.dto.PropertyItem;
import com.wuuxiang.polestar.admin.dto.UserDto;
import com.wuuxiang.polestar.comm.constant.Constant;

/**
 * 项目名称：polestar <br>
 * 类描述： <br>
 * 创建人：夏鸿鹏 <br>
 * 创建时间：2017-11-28 16:30 <br>
 * 修改备注： <br>
 */
@Component
public class UserService implements IUserService {
	@Autowired
	private IPropertyDao propertyDao;

	@Override
	public Boolean checkExists(String itemName) {
		return propertyDao.checkExists(ZKPaths.makePath(Constant.ROOT_NODE, Constant.USER_NODE, itemName));

	}

	@Override
	public void createUser(String itemName, String value) {
		propertyDao.createProperty(ZKPaths.makePath(Constant.ROOT_NODE, Constant.USER_NODE, itemName), value);

	}

	@Override
	public void removeUser(String itemName) {
		propertyDao.deleteProperty(ZKPaths.makePath(Constant.ROOT_NODE, Constant.USER_NODE, itemName));

	}

	@Override
	public void updateUser(String itemName, String value) {
		propertyDao.updateProperty(ZKPaths.makePath(Constant.ROOT_NODE, Constant.USER_NODE, itemName), value);

	}

	@Override
	public List<UserDto> getUserLst() {
		List<PropertyItem> lst = propertyDao.findProperties(ZKPaths.makePath(Constant.ROOT_NODE, Constant.USER_NODE));
		List<UserDto> retLst = new ArrayList<UserDto>();
		for (PropertyItem propertyItem : lst) {
			UserDto userDto = new UserDto();
			userDto.setUsername(propertyItem.getName());
			userDto.setPassword(propertyItem.getValue());
			retLst.add(userDto);
		}
		return retLst;
	}

	@Override
	public UserDto getUser(String itemName) {
		UserDto userDto = null;
		String password = propertyDao.getProperty(ZKPaths.makePath(Constant.ROOT_NODE, Constant.USER_NODE, itemName));
		if (StringUtils.isNotBlank(password)) {
			userDto = new UserDto();
			userDto.setUsername(itemName);
			userDto.setPassword(password);
		}
		return userDto;
	}

}
